package work.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import work.entity.Book;
import work.entity.Bookware;
import work.entity.Entering;
import work.service.BookService;
import work.service.BookwareService;
import work.service.EnteringService;
import work.vo.DataVo;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@RequestMapping("/entering")
public class EnteringHandler {

    @Autowired
    private EnteringService enteringService;
    @Autowired
    private BookwareService bookwareService;
    @Autowired
    private BookService bookService;

    // 新书入库
    // 前端传入entering对象，enteringId和enteringDate为空，后台会自动生成
    @PostMapping("/putin")
    public DataVo putInBookware(@RequestBody Entering entering) throws ParseException {
        DataVo dataVo = new DataVo();

        // 更新图书表
        Book book = new Book(entering.getBookId(), entering.getBookWareId(), "正常");
        if (bookService.insertBook(book) == 0) {
            dataVo.setCode(230);
            dataVo.setMsg("信息更新产生错误");
            return dataVo;
        }
        // 更新馆藏表，插入或更新
        int i = 0;
        int j = 0;

        if (bookwareService.findById(entering.getBookWareId()) == null) {
            Bookware bookware = new Bookware();
            bookware.setBookWareId(entering.getBookWareId());
            bookware.setBookName(entering.getBookName());
            bookware.setBookISBN(entering.getBookISBN());
            bookware.setBookAuthor(entering.getBookAuthor());
            bookware.setBookPublisher(entering.getBookPublisher());
            bookware.setBookType(entering.getBookType());
            bookware.setBookPrice(entering.getBookPrice());
            bookware.setBookTotal(1);
            bookware.setBookLending(0);
            bookware.setBookHistory(0);
            bookware.setBookGrade(6.80);
            i = bookwareService.insertBookware(bookware);
        } else {
            Bookware bookware = bookwareService.findById(entering.getBookWareId());
            bookware.setBookTotal(bookware.getBookTotal() + 1);
            j = bookwareService.updateWare(bookware);
        }
        if (i == 0 && j == 0) {
            dataVo.setCode(231);
            dataVo.setMsg("信息更新产生错误");
            return dataVo;
        }

        // 更新入库表
        entering.setEnteringId(String.valueOf(System.currentTimeMillis()));
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date now = new Date();
        entering.setEnteringDate(simpleDateFormat.parse(simpleDateFormat.format(now)));
        int x = enteringService.insertEntering(entering);
        if (x == 0) {
            dataVo.setCode(232);
            dataVo.setMsg("信息更新产生错误");
        } else {
            dataVo.setCode(233);
            dataVo.setMsg("新书入库成功");
        }
        return dataVo;
    }
}
